<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="受训人" prop="staffName">
            <el-input v-model="queryParams.staffName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="培训状态" prop="recevieStatus">
            <el-select v-model="queryParams.recevieStatus" placeholder="请选择" clearable
              @keyup.enter.native="handleQuery">
              <el-option v-for="dict in recevieStatusOptions" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="receiveList" border>
      <el-table-column label="受训人" align="center" prop="staffName" />
      <el-table-column label="岗位" align="center" prop="projectPostName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName" />
        </template>
      </el-table-column>
      <el-table-column label="培训状态" align="center" prop="recevieStatus">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.recevieStatus == 0">待提交</el-tag>
          <el-tag type="danger" v-if="scope.row.recevieStatus == 1">待审核</el-tag>
          <el-tag type="success" v-if="scope.row.recevieStatus == 2 || scope.row.recevieStatus == 3">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核结果" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.recevieStatus == 2">合格</el-tag>
          <el-tag v-if="scope.row.recevieStatus == 3">不合格</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="培训完成时间" align="center" prop="completeTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核完成时间" align="center" prop="recevieTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recevieTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核员反馈" align="center" prop="recevieResult" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="scope.row.recevieStatus == 0" @click="handleSumbit(scope.row, 1)"
            v-hasPermi="['cld:receive:submit']">提交</el-button>
          <el-button size="mini" type="text" v-if="scope.row.recevieStatus == 1"
            @click="handleAuditOrView(scope.row, 1)" v-hasPermi="['cld:receive:audit']">审核</el-button>
          <el-button size="mini" type="text" v-if="scope.row.recevieStatus == 2 || scope.row.recevieStatus == 3"
            @click="handleAuditOrView(scope.row, 2)" v-hasPermi="['cld:receive:view']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改受训人信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-descriptions class="margin-top" title="受训人" :column="3" :size="size" border>
          <el-descriptions-item v-if="form.trainType == 2">
            <template slot="label">
              项目名称
            </template>
            {{ form.projectName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              员工姓名
            </template>
            {{ form.staffName }}
          </el-descriptions-item>
          <el-descriptions-item v-if="form.trainType == 2">
            <template slot="label">
              职务
            </template>
            {{ form.jobName }}
          </el-descriptions-item>
          <el-descriptions-item v-if="form.trainType == 2">
            <template slot="label">
              岗位
            </template>
            <dict-tag :options="dict.type.cld_project_post" :value="form.projectPostName" />
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              性别
            </template>
            {{ form.staffSex }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              年龄
            </template>
            {{ calculateAge(form.staffIdcard) }}
          </el-descriptions-item>
        </el-descriptions>
        <el-divider content-position="left">反馈信息</el-divider>
        <el-form-item label="审核结果" prop="recevieStatus">
          <el-radio-group :disabled="!isAudit" v-model="form.recevieStatus">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="3">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="反馈信息" prop="recevieResult">
          <el-input :disabled="!isAudit" type="textarea" :rows="4" v-model="form.recevieResult" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="相关图片" prop="trainImg">
          <image-upload v-model="form.trainImg" v-if="isAudit" :limit="1" />
          <image-preview v-if="!isAudit" :src="form.trainImg" :width="100" :height="100" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceive, getReceive, delReceive, addReceive, updateReceive } from "@/api/cld/receive";
import { recevieStatusOptions } from '@/assets/staticDict'
export default {
  name: "Receive",
  dicts: ['cld_project_post'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 受训人信息表格数据
      receiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainPlanId: this.$route.params.trainPlanId,
        staffName: null,
        recevieStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recevieStatus: [
          { required: true, message: "审核结果不能为空", trigger: "change" }
        ],
        recevieResult: [
          { required: true, message: "反馈信息不能为空", trigger: "blur" }
        ],
        trainImg: [
          { required: true, message: "请上传相关图片", trigger: "blur" }
        ],
      },
      // 受训人审核结果
      recevieStatusOptions: recevieStatusOptions,
      // 是否审核状态
      isAudit: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询受训人信息列表 */
    getList() {
      this.loading = true;
      listReceive(this.queryParams).then(response => {
        this.receiveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        trainReceiveId: null,
        trainPlanId: null,
        staffId: null,
        recevieStatus: null,
        recevieResult: null,
        trainImg: null,
        recevieTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateReceive(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 审核or查看
    handleAuditOrView(row, type) {
      this.reset();
      getReceive(row.trainReceiveId).then(response => {
        if (type == 1) {
          this.title = "审核";
          this.isAudit = true;
        } else {
          this.title = "查看";
          this.isAudit = false;
        }
        this.form = response.data;
        this.form.recevieStatus = 2;
        this.open = true;

      });
    },
    // 提交
    handleSumbit(row) {
      this.$confirm('提交表示该员工培训已完成，等待审核员考核?', '确认提交这条信息吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let form = {
          trainReceiveId: row.trainReceiveId,
          recevieStatus: 1
        }
        updateReceive(form).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getList();
        })
      })
    },
    // 返回
    handleReturn() {
      const obj = { path: "/cloudManage/personnel/iecturer", name: "iecturer" };
      this.$tab.closeOpenPage(obj);
      this.$tab.refreshPage(obj);
    }
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
